<?php
/* Begin Widget Class */
class DT_testimonials extends WP_Widget {

	/* Widget setup  */
	function DT_testimonials() {
		/* Widget settings. */
		$widget_ops = array(
            'classname'     => 'dt_testimonials',
            'description'   => __('Here some testimonial functionality', 'dt')
        );

		/* Widget control settings. */
		$control_ops = array(
            'width'     => 250,
            'height'    => 250,
            'id_base'   => 'dt_testimonials'
        );

		/* Create the widget. */
		$this->WP_Widget(
            'dt_testimonials',
            __( THEME_TITLE.' Testimonials', 'dt' ),
            $widget_ops,
            $control_ops
        );
	}

	/* Display the widget  */
	function widget( $args, $instance ) {
		extract( $args );
        
		// get testimonials all with status publish
        $dt_query = new Wp_Query(
            array(
                'post_type'         => 'dt_testimonials',
                'posts_per_page'    => -1,
                'post_status'       => 'publish'
            )
        );
        $dt_posts = $dt_query->posts;
        
        /* Our variables from the widget settings. */
		$title = apply_filters('widget_title', $instance['title'] );
        
        // prepare widget content
        $layout = $content = '';
        
        // widget layouts
        switch( $id ) {
            case 'bottom-widget-area':
                $layout = '%1$s%2$s<div class="sh">%3$s</div>';
                $content_layout = '<div class="item%1$s">%3$s<div class="by_author">%2$s</div></div>';
                break;
            default:
                $layout = '%2$s<div class="widget">%1$s<div class="sh">%3$s</div></div><div class="widget_b"></div>';
                $content_layout = '<div class="item%1$s"><h5>%2$s</h5>%3$s</div>';
//                break;
        }
        
        if( $title ) {
            $title = $before_title . $title . $after_title;
        }
 
        $arrows = <<<HERE
        <div class="widget_arr">
            <a href="#" class="larr"></a>
            <a href="#" class="rarr"></a>
        </div>
HERE;
        
        if( $dt_posts ) {
            for( $i = 0, $class = ' first'; isset($dt_posts[$i]); $i++, $class = '' ) {
                $content .= sprintf(
                    $content_layout,
                    $class,//1
                    $dt_posts[$i]->post_title,//2
                    apply_filters( 'the_content', $dt_posts[$i]->post_content )//3
                );
            }          
        }
        
        echo $before_widget ;
        
        echo sprintf(
            $layout,
            $arrows,//1
            $title,//2
            $content//3
        );
        
        echo $after_widget;
	}

	/* Update the widget settings  */
	function update( $new_instance, $old_instance ) {
		$instance = $old_instance;
        $instance['title'] = $new_instance['title'];
      
		return $instance;
	}

	/**
	 * Displays the widget settings controls on the widget panel.
	 * Make use of the get_field_id() and get_field_name() function
	 * when creating your form elements. This handles the confusing stuff.
	 */
	function form( $instance ) {

		/* Set up some default widget settings. */
		$defaults = array( 
			'title' => ''
        );

		$instance = wp_parse_args( (array) $instance, $defaults );
    ?>
		
		<p>
			<label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e('Title:', 'dt'); ?></label>
			<input id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" value="<?php echo $instance['title']; ?>" style="width:85%;" />
		</p>
				
		<div style="clear: both;"></div>
	<?php
	}
}

function dt_testimonials_register() {
	register_widget( 'DT_testimonials' );
}

add_action( 'widgets_init', 'dt_testimonials_register' );
